<?php

/*

Template Name: Register

*/

//Logic to save records
global $wp_query;
$current_user = wp_get_current_user();

$user_id = $current_user->ID;
$rubro_id = get_user_meta($user_id, 'rubro_profile', true);

$start_date = 2010;
$end_date = date('Y'); 
$year = $wp_query->query_vars['page'];
if($year == 0){
  $year = date('Y');
}

$prev = "off";
$next = "off";
$prev_link = "#";
$next_link = "#";
if($year<$end_date){
   $next = "on";
   $next_link = get_permalink().($year+1);
}
if($year>$start_date){
  $prev = "on";
  $prev_link =  get_permalink().($year-1);
}

//Grabar consumo
/*if(isset($_POST['consumo']) && $_POST['consumo'] == 'submit'){
  //data
  $usuario = $user_id;
  $anio = $year;
  $mes = $_POST['mes'];
  $cantidad = $_POST['cantidad'];
  $precio = $_POST['precio'];
  $tipo = $_POST['tipo'];
  $subtipo = $_POST['subtipo'];  
  $total_meses = count( $mes );
  for ( $i = 0; $i < $total_meses; $i++ ) {
      $wpdb->query('UPDATE 
            `bpa_registry_consumption` 
        SET 
            `quantity` = '. $cantidad[$i] .', 
            `price` = '. $precio[$i] .'
        WHERE 
            `user_id` = '. $usuario .' AND 
            `type_id` = '. $tipo .' AND 
            `subtype_id` = '. $subtipo[$i] .' AND 
            `year` = '. $anio .' AND 
            `month` = '. $mes[$i]);
  }
}*/

//recuperamos su consumo del anio
$consumos = $wpdb->get_results('SELECT DISTINCT a.id, a.user_id, a.type_id, a.subtype_id, a.year, a.month, a.quantity, a.price, 
                                                (select number from bpa_registry_workers where year = a.year and  user_id = a.user_id and month = a.month) as workers
                                FROM 
                                    bpa_registry_consumption as a 
                                where a.user_id = '.$user_id.'  
                                    and a.year = '.$year.' 
                                ORDER BY 
                                a.month, a.subtype_id ASC' 
                            , ARRAY_A);
//recuperamos los subtipos

$consumos = array_reverse($consumos);
$array_subtipos = array(
    1 => "Gasolina",
    2 => "Diesel",
    3 => "GLP",
    4 => "GNV",
);

get_template_part('secondary', 'header');
get_template_part('secondary','sidebar');

//obtiene los meses del anio
$months = get_months_till_current_array($year);



?>

<script src="<?php echo get_template_directory_uri(); ?>/js/lightbox.min.js"></script>
<script src="<?php echo get_template_directory_uri(); ?>/js/popup.js"></script>
<link href="<?php echo get_template_directory_uri(); ?>/css/lightbox.css" rel="stylesheet">

<div id="dash-content">

    <?php if (have_posts()) : ?> 
    <?php while (have_posts()) : the_post(); ?>

    <header>
        <div class="register-title">
            <h2><?php the_title();?></h2>
        </div>

        <div class="register-year-selector">
            <div class="label">Elija el a&ntilde;o a editar:</div>
            <a class="previous" href="<?php echo $prev_link; ?>">
                <img src="<?php echo get_template_directory_uri(); ?>/images/prev_<?php echo $prev; ?>.png" />
            </a>
            <input class="year-input" type="text" readonly="readonly" value="<?php echo $year;?>" style="width:50px;"/>
            <a class="next" href="<?php echo $next_link; ?>">
                <img src="<?php echo get_template_directory_uri(); ?>/images/next_<?php echo $next; ?>.png" />
            </a>
        </div>
        <h5><?php the_content();?></h5>
    </header> 

    <div class="info">
        
        <div class="columna-izq">
            <h3>Agua</h3>
            <p>Registre su consumo de agua usando los campos provistos abajo.</p>
            <p style="padding-top:-10px;padding-bottom:10px">
                <a class="buton b-gris" rel="lightbox"   href="<?php echo get_template_directory_uri(); ?>/images/ecopyme-como-leer-recibo-agua.jpg">¿C&oacute;mo leer su recibo de agua?</a>
            </p>
            <form class="form-a" id='form-water' method='POST'>
                <input type='hidden' name='tipo' value='1'>
                <input type='hidden' name='year' value='<?php echo $year; ?>'/>
                <div id="water_container">
                    <div class="box-head">
                        <div class="box-month">Mes</div>
                        <div class="box-work">Trabajadores</div>
                        <div class="box-input">m<sup>3</sup> </div>
                        <div class="box-input">Soles</div>
                    </div>
                    <?php foreach ( $consumos as $consumos_user ) { 
                            if($consumos_user['type_id']==1){ ?>
                                <input type='hidden' name='counter' value='1'>
                                <input type='hidden' name='mes' value='<?php echo $consumos_user['month'] ?>'>
                                <input type='hidden' name='subtipo' value='0'> <!-- NO tiene= 0 -->
                                <div class="box-items">
                                    <div class="box-month"><?php echo $months[$consumos_user['month']] ?></div>
                                    <div class="box-work"><?php echo $consumos_user['workers'] ?></div>
                                    <div class="box-input"><input class="mask_decimal" value="<?php echo $consumos_user['quantity'] ?>" type="text" name="cantidad" title="m3"></div>
                                    <div class="box-input"><input class="mask_decimal" value="<?php echo $consumos_user['price'] ?>" type="text" name="precio" title="S/."></div>
                                </div>
                    <?php }                        
                    }; ?>
                </div>
                <a class="buton mini-buton" id="save_water_record">Grabar</a>
            </form>
            <div id="water_save_message"></div>
        </div>


        <div class="columna-dch">
                  
            <h3>Energ&iacute;a</h3>
            <p>Registre sus consumos de electricidad y combustible usando los campos provistos abajo.</p>
            <h4>Electricidad</h4>
            <p style="padding-top:-10px;padding-bottom:10px">
                <a class="buton b-gris" rel="lightbox"  href="<?php echo get_template_directory_uri(); ?>/images/ecopyme-como-leer-recibo-electricidad.jpg">¿C&oacute;mo leer su recibo de electricidad?</a>
            </p>

            <form class="form-a" id="form-electricity" method="POST">
                <input type='hidden' name='consumo' value='submit'/>
                <input type='hidden' name='tipo' value='2'/>
                <input type='hidden' name='year' value='<?php echo $year; ?>'/>
                <div id="electricity_container">
                    <div class="box-head">
                        <div class="box-month">Mes</div>
                        <div class="box-work">Trabajadores</div>
                        <div class="box-input">kWh</div>
                        <div class="box-input">Soles</div>
                    </div>
                    <?php foreach ( $consumos as $consumos_user ) { 
                            if($consumos_user['type_id']==2){ ?>
                                <input type='hidden' name='mes' value='<?php echo $consumos_user['month'] ?>'/>
                                <input type='hidden' name='subtipo' value='0'/>
                                <div class="box-items">
                                    <div class="box-month"><?php echo $months[$consumos_user['month']] ?></div>
                                    <div class="box-work"><?php echo $consumos_user['workers'] ?></div>
                                    <div class="box-input"><input value="<?php echo $consumos_user['quantity'] ?>" class="mask_decimal" name="cantidad" placeholder="kWh" title="kWh"></div>
                                    <div class="box-input"><input value="<?php echo $consumos_user['price'] ?>" class="mask_decimal" name="precio" placeholder="S/." title="S/."></div>
                                </div>
                    <?php }                        
                    }; ?>
                </div>
                <a class="buton mini-buton" id="save_electricity_record">Grabar</a>
            </form> 
            <div id="electricity_save_message"></div>
        </div>
        <div class="col-cent">
            <h4>Combustible</h4>
            <p style="padding-top:-10px;padding-bottom:10px">
                <a class="buton b-gris" rel="lightbox"  href="<?php echo get_template_directory_uri(); ?>/images/combustible-conversion.png">Conversión de contenido de balones de GLP a litros</a>
            </p>
            <form class="form-b" id='form-gas' method='POST'>
                <input type='hidden' name='consumo' value='submit'>
                <input type='hidden' name='tipo' value='3'> <!-- Commbustible = 3 -->
                <input type='hidden' name='year' value='<?php echo $year; ?>'/>
                <div id="gas_container">
                    <div class="box-head">
                        <div class="box-month">Mes</div>
                        <div class="box-work">Trabajadores</div>
                        <div class="box-type">Tipo</div>
                        <div class="box-galones">Cantidad</div>
                        <div class="box-input">Soles</div>
                    </div>
                    <?php foreach ( $consumos as $consumos_user ) { 
                            if($consumos_user['type_id']==3){ ?>
                                <input type='hidden' name='mes' value='<?php echo $consumos_user['month'] ?>'>
                                <input type='hidden' name='subtipo' value='<?php echo $consumos_user['subtype_id'] ?>'> <!-- Commbustible = 3 -->
                                <div class="box-items">
                                    <div class="box-month"><?php echo $months[$consumos_user['month']] ?></div>
                                    <div class="box-work"><?php echo $consumos_user['workers'] ?></div>
                                    <div class="box-type"><?php echo $array_subtipos[$consumos_user['subtype_id']] ?></div>
                                    <div class="box-galones"><input value="<?php echo $consumos_user['quantity'] ?>" class="mask_decimal" name="cantidad" placeholder="Galones" title="Galones">
                                        <strong class="medida">
                                            <?php 
                                            $subtype = $consumos_user['subtype_id'];
                                            if ($subtype == 1 or $subtype == 2) {
                                                echo "Gal";
                                            }else if($subtype == 3){
                                                echo "l";
                                            }else if($subtype == 4)
                                                echo "m<sup>3</sup>";
                                            ?>
                                        </strong>
                                    </div>
                                    <div class="box-input"><input value="<?php echo $consumos_user['price'] ?>" class="mask_decimal" name="precio" placeholder="S/." title="S/."></div>
                                </div>
                    <?php }                        
                    }; ?>
                </div>
                <a class="buton mini-buton" id="save_gas_record">Grabar</a>
            </form>
            <div id="fuel_save_message"></div>
          </div>  
      
        <div class="data-export">
            Para exportar los datos haga <a class="bolded" href="<?php echo get_template_directory_uri();?>/export-excel/generator.php">click aqu&iacute;</a>
        </div>

    </div>

    <?php endwhile;?>

    <?php endif; ?>

</div>          

<?php get_template_part('secondary','footer'); ?>